from fastapi import FastAPI, HTTPException
from pydantic import BaseModel
from typing import Optional
import json
from receiveEmail import get_json_data as get_json_data

from fastapi.middleware.cors import CORSMiddleware

# 创建 FastAPI 应用实例
app = FastAPI()

app.add_middleware(
    CORSMiddleware,
    allow_origins=["*"],  # 允许所有来源
    allow_credentials=True,
    allow_methods=["*"],
    allow_headers=["*"],
)


# 定义请求体模型
class RequestBody(BaseModel):
    input_string: str

# 定义 POST 接口 '/getFicoData'
@app.post("/getFicoData")
async def get_fico_data(request_body: RequestBody):
    # 获取请求中的字符串参数
    input_string = request_body.input_string
    
    # 检查输入是否为空
    if not input_string or input_string.strip() == "":
        raise HTTPException(status_code=400, detail="Input string cannot be empty")
    
    # 返回处理后的数据（这里简单返回输入字符串）
    jsonData = get_json_data(input_string)

    # with open('./files/extracted/微信支付账单(20250101-20250328)/微信支付账单(20250101-20250328).json', 'r', encoding='utf-8') as file:
    #     jsonData = json.load(file)  # 将 JSON 数据解析为 Python 对象

    return {
        "status": "success",
        "received_data": jsonData,
        "message": "Data received successfully!"
    }

# 主函数：启动服务
if __name__ == "__main__":
    import uvicorn
    # 启动 FastAPI 服务，监听端口 8070
    uvicorn.run(app, host="0.0.0.0", port=8070)